package com.teach.food.controller;

import java.io.IOException;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.teach.R;
import com.teach.dto.FoodGoodsDTO;
import com.teach.dto.LoginUser;
import com.teach.entity.Shops;
import com.teach.food.entity.FoodGoods;
import com.teach.food.service.FoodGoodsService;
import com.teach.mapper.FoodGoodsMapper;
import com.teach.service.FileService;
import com.teach.service.ShopsService;

import lombok.extern.slf4j.Slf4j;
/**
 * 食物商品控制器
 * @author DELL
 *
 */
//@Controller
@RestController
@RequestMapping("/food/foodgoods")
@Slf4j
public class FoodGoodsController {
	@Autowired
	private FoodGoodsService service;
	@Autowired
	private ShopsService sservice;
	@Autowired
	private FoodGoodsMapper mapper;
	@Autowired
	private FileService fservice;
	@RequestMapping("add.do")
	//@ResponseBody
	public R doAdd(String foodGoodsName,Float foodGoodsPrice,String foodGoodsDescribes,MultipartFile file,LoginUser userName,Shops shops) {
		
		FoodGoods f = new FoodGoods();
		f.setFoodGoodsName(foodGoodsName);
		f.setFoodGoodsPrice(foodGoodsPrice);
		f.setFoodGoodsDescribes(foodGoodsDescribes);
		f.setFoodGoodsImg(fservice.save(file));
		shops = sservice.getByUserName(userName.getUserName());
		Long id = shops.getId();
		f.setFoodGoodsMerchantId(id);
		service.add(f);
		return R.ok();
		
	}
	//@ResponseBody
	@RequestMapping("update.do")
	public R doUpdete(Integer foodGoodsId,String foodGoodsName,Float foodGoodsPrice,String foodGoodsDescribes,MultipartFile file,Long foodGoodsMerchantId) {
		FoodGoods f = new FoodGoods();
		f.setFoodGoodsId(foodGoodsId);
		f.setFoodGoodsName(foodGoodsName);
		f.setFoodGoodsDescribes(foodGoodsDescribes);
		f.setFoodGoodsPrice(foodGoodsPrice);
		f.setFoodGoodsImg(fservice.save(file));
		f.setFoodGoodsMerchantId(foodGoodsMerchantId);
		service.modify(f);
		return R.ok();
	}
	
	//@ResponseBody
	@RequestMapping("delete.do")
	public R doDelete(Integer foodGoodsId) {
		service.delete(foodGoodsId);
		return R.ok();
	}
	
	//@ResponseBody
	@RequestMapping("page.do")
	public Object dopage(FoodGoodsDTO search) {
		return service.page(search);
	}
	//@ResponseBody
	@RequestMapping("list.do")
	public Object dolist() {
		return service.list();
	}
	@RequestMapping("getlist.do")
	public Object dogetlist(LoginUser userName,Shops shops) {
		shops = sservice.getByUserName(userName.getUserName());
		return mapper.selectByfoodGoodsMerchantId(shops.getId());
	}
}
